﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace HDK.API.Syndication
{
    [DataContract(IsReference = true)]
    public class SyndicationFeedBase : ISyndicationFeed
    {
        #region Constructor

        /// <summary>
        /// Initialize a new instance of the <see cref="RSSFeedReader.Data.Models.FeedBase"/> class.
        /// </summary>
        public SyndicationFeedBase()
        {
            //Images = new List<string>();
            this.Authors = new List<ISyndicationPerson>();
            this.Categories = new List<ISyndicationCategory>();
            this.Contributors = new List<ISyndicationPerson>();
            this.Posts = new List<ISyndicationPost>();

        }

        /// <summary>
        /// Initialze a new instance of the <see cref="RSSFeedReader.Data.Models.FeedBase"/> class.
        /// </summary>
        /// <param name="title">The title of the feed.</param>
        /// <param name="description">The description of the feed.</param>
        /// <param name="link">The link to the feed.</param>
        public SyndicationFeedBase(string title, string description, string link)
            : this()
        {
            Title = title;
            Description = description;
            Link = link;
        }

        #endregion

        [DataMember]
        public string ImageUrl { get; set; }

        private IList<string> m_images;

        [DataMember]
        public IList<string> Images
        {
            get
            {
                if (m_images == null)
                    m_images = GetImages();
                return m_images;
            }
            set { m_images = value; }
        }

        private IList<string> GetImages()
        {
            return new List<string>();
            //ImageDataListSource dataListSource = new ImageDataListSource(this);
            //return (IList<string>)new VirtualizingDataList<string>(dataListSource);
        }

        //public class ImageDataListSource : DataListSourceBase<string>
        //{
        //    private ISyndicationFeed m_api;
        //    List<string> tmpList;

        //    public ImageDataListSource(ISyndicationFeed feed)
        //    {
        //        m_api = feed;
        //    }

        //    public async override Task<int?> GetCountAsync()
        //    {
        //        if (tmpList == null)
        //        {
        //            tmpList = new List<string>();

        //            tmpList.Add(m_api.ImageUrl);
        //            tmpList.AddRange(m_api.Description.ExtractHTMLImages());

        //            foreach (var post in m_api.Posts)
        //                tmpList.AddRange(post.Content.ExtractHTMLImages());


        //            tmpList = tmpList.Where(u=>!string.IsNullOrEmpty(u)).Distinct().ToList();

        //        }
        //        return tmpList.Count;
        //    }

        //    public async override Task<string> GetItemAsync(int index)
        //    {
        //        return tmpList[(int)index];
        //    }
        //}

        #region Public Properties


        [DataMember]
        public string Title { get; set; }

        /// <summary>
        /// Gets or sets the description of the feed.
        /// </summary>
        [DataMember]
        public string Description { get; set; }

        /// <summary>
        /// Gets or sets link to the feed.
        /// </summary>
        [DataMember]
        public string Link { get; set; }

        [DataMember]
        public IList<ISyndicationPerson> Authors { get; set; }

        [DataMember]
        public IList<ISyndicationPerson> Contributors { get; set; }

        [DataMember]
        public IList<ISyndicationCategory> Categories { get; set; }

        [DataMember]
        public DateTimeOffset LastUpdatedTime { get; set; }

        /// <summary>
        /// Gets or sets the List of type <see cref="RSSFeedReader.Data.Models.Post"/> belonging to this instance.
        /// </summary>
        [DataMember]
        public IList<ISyndicationPost> Posts
        {
            get;
            set;
        }

        /// <summary>
        /// Returns the title of the feed.
        /// </summary>
        /// <returns>The Title of the feed.</returns>
        public override string ToString()
        {
            return Title;
        }

        #endregion
    }

}
